#include <stdio.h>
#include <DynamicArray.h>
#include <stdbool.h>
#include <unistd.h>
#include "Scene.h"
#include "Player.h"
#include "monster.h"

bool Battle(player *p, monster *m)
{
    
    printf("你遭遇到了|%s|，血量🤍：|%d|，攻击力⚔️ ：|%d|\n",m->name.string,m->hp,m->attack);
    //让程序休眠
    sleep(1);
    while(1)
    {
        //清屏
        system("clear");
        PrintPlayer(p);
        printf("敌方信息：    名称：|%4s| 血量🤍 :|%3d| 攻击力⚔️ :|%d|\n",m->name.string,m->hp,m->attack);

        printf("请做出你的选择：\n");
        printf("1、进攻🗡\n");
        printf("2、暴击⚔\n");
        printf("3、逃跑🏃\n");
        int choice = 0;
        //清除缓冲区的换行符
        while(getchar() != '\n');
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("你对|%s|发起了进攻🗡 ，造成了%d点伤害🩸\n",m->name.string,p->attack);
                m->hp -= p->attack;
                if(m->hp <= 0)
                    break;
                printf("|%s|对你发起了反击🗡 ，造成了%d点伤害🩸\n",m->name.string,m->attack);
                p->hp -= m->attack;
                break;
            case 2:
                if(GetRand(3) == 1)
                {
                    printf("你对|%s|造成了暴击伤害⚔ ！造成了%d点伤害🩸\n",m->name.string,p->attack * 2);
                    m->hp -= 2 * p->attack;
                    if(m->hp <= 0)
                    break;
                    printf("|%s对你发起了反击🗡 ，造成了%d点伤害🩸\n",m->name.string,m->attack);
                    p->hp -= m->attack;
                }
                else
                {
                    printf("暴击⚔ 失败！未造成伤害！\n");
                    printf("|%s|对你发起了反击🗡 ，造成了%d点伤害🩸\n",m->name.string,m->attack);
                    p->hp -= m->attack;
                }
                break;
            case 3: 
                if(IsEqual(&m->name,"黑龙") == true)
                {
                    printf("你面对的是本游戏boss☣ ，禁止逃跑❗\n");
                    printf("|%s|☣对你发起了攻击🗡 ，造成了%d点伤害🩸\n",m->name.string,m->attack);
                    p->hp -= m->attack;
                    break;
                }
                if(GetRand(2) == 1)
                {
                    printf("你逃跑成功了！\n");
                    sleep(1);
                    return true;//战斗结束，玩家存活
                }
                else
                {
                    printf("你逃跑失败！\n");
                    printf("|%s对你发起了反击🗡，造成了%d点伤害🩸\n",m->name.string,m->attack);
                    p->hp -= m->attack;
                }
                break;
            default:
                break;
        }

        sleep(1);
        if(p->hp <= 0)
        {
            p->hp = 0;
            printf("战斗失败！\n");
            return false;
        }

        if(m->hp <= 0)
        {
            m->hp = 0;
            printf("战斗胜利！\n");
            printf("怪物掉落%d点经验🥇，%d枚金币💰\n",m->exp,m->golden);
            p->exp += m->exp; 
            p->golden += m->golden;
            return true;
        }
    }

}


int main()
{
    //初始化场景
    char Map[MAPSIZE][MAPSIZE] = {0};
    InitMap(Map);

    //初始化玩家
    player p;
    InitPlayer(&p);
    Map[p.x][p.y] = p.symbol;


    //初始化怪兽
    INITDARRY(monsters);
    SetMonsters(&monsters);
    SetMonsterPosition(&monsters,Map);

    PrintMap(Map);
    monster *posM = NULL;
    while(1)
    {
        PrintPlayer(&p);
        PlayerMove(&p,Map);
        if(posM != NULL)
        {
            Map[posM->x][posM->y] = posM->symbol;
            posM == NULL;
        }
        monster *m = GetMonsterByPosition(&monsters,p.x,p.y);
        if(m != NULL)
        {
            if(Battle(&p,m) == false)
            {
                printf("你阵亡了👻！游戏结束！\n");
                sleep(1);
                return -1;
            }
            LevelUp(&p);
            if(m->hp == 0)
            {
                if(IsContains(&m->name,"黑龙") == true)
                {
                    printf("你击败了boss黑龙☣，游戏结束！\n");
                    return 0;
                }
                printf("|%s|已被你击败！\n",m->name.string);
                Map[m->x][m->y] = p.symbol;
                sleep(1);
            }
            else
            {
                posM = m;
            }
        }
        PrintMap(Map);
    }
    return 0;

}